﻿//+---------------------------------------------
//| js工具类
//| Create By:2009-4-20
//| Author:YuBing
//+---------------------------------------------


//+---------------------------------------------
//| 将Date/String类型,解析为String类型. 
//| 传入String类型,则先解析为Date类型 
//| 不正确的Date,返回 '' 
//| 如果时间部分为0,则忽略,只返回日期部分. 
//+---------------------------------------------
function formatDate(v){  
    if(typeof v == 'string') v = parseDate(v);  
    if(v instanceof Date){  
     var y = v.getFullYear();  
     var m = v.getMonth() + 1;  
     var d = v.getDate();  
     var h = v.getHours();  
     var i = v.getMinutes();  
     var s = v.getSeconds();  
     var ms = v.getMilliseconds();     
     if(ms>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s + '.' + ms;  
     if(h>0 || i>0 || s>0) return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s;  
     return y + '-' + m + '-' + d;  
    }  
    return '';  
}  



//+---------------------------------------------------  
//| 字符串转成日期类型   
//| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd  
//+---------------------------------------------------  
function stringToDate(DateStr) {   
     if (DateStr == '' || DateStr == 'undefined') {
        return null;
     }
     var converted = Date.parse(DateStr);  
     var myDate = new Date(converted);  
     if (isNaN(myDate))  
     {   
         //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';  
         var arys= DateStr.split('-');  
         myDate = new Date(arys[0],--arys[1],arys[2]);  
     }  
     return myDate;  
}

//+------------------------------------------------------------
//| 获取一个随机数
//+------------------------------------------------------------
function getRandomNumber() {
    var Timer = new Date();
    var seconds = Timer.getSeconds();
    var randomnumber = Math.floor(Math.random()*10000)+seconds;
    return randomnumber;
}

//+---------------------------------------------------  
//| 比较两个时间的大小   
//| time1 < time2 return -1;
//| time1 == time2 return 0;
//| time1 > time2 return 1;
//+--------------------------------------------------- 
function compareDate(time1, time2) {
    var i = time1 - time2;
    if (i < 0)
        return -1;
    if (i > 0)
        return 1;
    return 0;
}



//+---------------------------------------------------  
//| request对象
//+--------------------------------------------------- 
function request(strName) {
    var strHref = window.document.location.href;
    var intPos = strHref.indexOf("?");
    var strRight = strHref.substr(intPos + 1);

    var arrTmp = strRight.split("&");
    for(var i = 0; i < arrTmp.length; i++)
    {
        var arrTemp = arrTmp[i].split("=");

        if(arrTemp[0].toUpperCase() == strName.toUpperCase()) return arrTemp[1];
    }
    return "";
}



//+---------------------------------------------------  
//| string.Trim(),htmlEncode(),htmlDecode()
//+--------------------------------------------------- 
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.lTrim = function() {
    return this.replace(/(^\s*)/g, "");
}

String.prototype.rTrim = function() {
    return this.replace(/(\s*$)/g, "");
}

String.prototype.htmlEncode = function() {  
    var s = "";  
    if (this.length == 0) return "";  
    s = this.replace(/&/g, "&amp;");  
    s = s.replace(/</g,  "&lt;");  
    s = s.replace(/>/g,  "&gt;");  
    s = s.replace(/ /g,  "&nbsp;");  
    s = s.replace(/\'/g,   "&#39;");  
    s = s.replace(/\"/g,   "&quot;");  
    s = s.replace(/\r\n/g,   "<br>");  
    return s;  
}  
String.prototype.htmlDecode = function() {  
    var s = "";  
    if (this.length == 0) return "";  
    s = this.replace(/&amp;/g, "&");  
    s = s.replace(/&lt;/g,  "<");  
    s = s.replace(/&gt;/g,  ">");  
    s = s.replace(/&nbsp;/g,  " ");  
    s = s.replace(/&#39;/g,   "\'");  
    s = s.replace(/&quot;/g,   "\"");  
    s = s.replace(/<br>/g,   "\r\n");  
    s = s.replace(/&#183;/g, "·");
    return s;  
}   
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 




//+---------------------------------------------------  
//| Cookie函数
//+--------------------------------------------------- 
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}

	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
} 

function setCookie( name, value, expires, path, domain, secure ) {

	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
	( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + 
	( ( path ) ? ';path=' + path : '' ) +
	( ( domain ) ? ';domain=' + domain : '' ) +
	( ( secure ) ? ';secure' : '' );

} 

function deleteCookie( name, path, domain ) {

	if ( getCookie( name ) ) document.cookie = name + '=' +
		( ( path ) ? ';path=' + path : '') +
		( ( domain ) ? ';domain=' + domain : '' ) +
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';

} 
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 



//+---------------------------------------------------  
//| Array.strip()
//+--------------------------------------------------- 
Array.prototype.container=function(element){
    for (var i=0; i<this.length; i++) {
        if (this[i] == element)
            return true;
    }
    return false;
}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 


//+---------------------------------------------------  
//| dom对象绑定事件，可带参数绑定，支持IE6+,FF1+
//+--------------------------------------------------- 
function Handler() { };
Handler.prototype = {
	/*
	* 把eventType类型的事件绑定到element元素，并使用handler事件句柄进行处理
	* 兼容 IE 及 Firefox 等浏览器
	*
	* @param element 在其上注册事件的对象（Object）
	* @param eventType 注册的事件类型（String），不加“on”
	* @param handler 事件句柄（Function）
	*/
	registerEvent : function(element, eventType, handler) {
		if(element.attachEvent) { //2级DOM的事件处理
		element.attachEvent('on'+ eventType, handler);
		}else if (element.addEventListener) {
		element.addEventListener(eventType, handler, false);
		} else { //0级DOM的事件处理
		element['on'+ eventType] = handler;
		}
	},
	/*
	* 获得带参数的事件句柄的引用
	*
	* @param obj 需要绑定事件处理函数的所有者，null 表示 window 对象
	* @param func 需要绑定的事件处理函数名
	* @param ... 第三个参数开始为绑定事件处理函数的参数，由 0 到多个构成
	*/
	bind: function(obj, handler) {
		obj = obj || window;
		var args = [];
		for(var i =2; i < arguments.length; i++)
		{
		args.push(arguments[i]);
		}
		return function() { handler.apply(obj, args) };
	}
}
//+---------------------------------------------------  
//| End
//+--------------------------------------------------- 

//+---------------------------------------------------  
//| Javascript Array 的 indexOf() 方法 
//+--------------------------------------------------- 
if (!Array.prototype.indexOf) 
{     
    Array.prototype.indexOf = function(elt /*, from*/)     
    {         
        var len = this.length;          
        var from = Number(arguments[1]) || 0;         
        from = (from < 0)? Math.ceil(from): Math.floor(from);         
        if (from < 0)             
            from += len;          
        for (; from < len; from++)         
        {             
            if (from in this && this[from] === elt)                 
            return from;         
        }         
        return -1;    
    }; 
}

